package com.zhuchang.self_rpc.test.democoding;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author RenPu
 * @Date 2022/2/7 16:48
 * @Version 1.0
 * @Description: 使用 FileChannel（通道）和方法 read、write，完成文件的拷贝
 **/
public class FileContentCopy {

    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("d:\\test.txt");
        FileChannel fileChannel01 = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("d:\\test2.txt");
        FileChannel fileChannel02 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        while (true){
            byteBuffer.clear();
            int read = fileChannel01.read(byteBuffer);
            if(read==-1){
                break;
            }

            byteBuffer.flip();
            fileChannel02.write(byteBuffer);

        }
        fileChannel01.close();
        fileChannel02.close();
    }
}
